package com.ticket.sass.admin.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.order.OrderTicketListInput;
import com.ticket.sass.admin.mapper.OrderTicketMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.admin.util.CinemaIdUtil;
import com.ticket.sass.common.entity.tenant.OrderTicketEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class OrderTicketServiceImpl extends ServiceImpl<OrderTicketMapper, OrderTicketEntity> implements BaseService<OrderTicketEntity> {

    public IPage<OrderTicketEntity> iPage(OrderTicketListInput input) {
        // 创建 Page 对象
        Page<OrderTicketEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        
        // 创建 QueryWrapper 对象
        QueryWrapper<OrderTicketEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(input.getOrderNo()), "o.order_no", input.getOrderNo());
        queryWrapper.eq("o.cinema_id", CinemaIdUtil.getCinemaIdFromRequest());
        queryWrapper.eq("o.type", 1);
        queryWrapper.orderByDesc("o.id");
        
        queryWrapper.ne("o.pay_status", 0);
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayStatusList()), "o.pay_status", input.getPayStatusList());
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayTypes()), "o.pay_type", input.getPayTypes());
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getStartDate() != null && input.getEndDate() != null) {
            LocalDateTime startOfDay = input.getStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("o.created_at", startOfDay, endOfDay);
        }
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getSessionStartDate() != null && input.getSessionEndDate() != null) {
            LocalDateTime startOfDay = input.getSessionStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getSessionEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("s.start_time", startOfDay, endOfDay);
        }
        
        queryWrapper.in(input.getUserIds().size() > 0, "ot.user_id", input.getUserIds());
        queryWrapper.in(input.getIds().size() > 0, "o.id", input.getIds());
        queryWrapper.isNull("o.deleted_at");
        queryWrapper.isNull("ot.deleted_at");
        return this.baseMapper.queryPage(page, queryWrapper);
    }
    
    
    public OrderTicketEntity getOneByOrderId(Long orderId) {
        // 创建 Page 对象
        
        // 创建 QueryWrapper 对象
        QueryWrapper<OrderTicketEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper);
    }
    
    public List<Map<String, Object>> getTicketStatsByCinemaId(Long cinemaId, LocalDate startDate, LocalDate endDate){
        return this.baseMapper.getFilmSaleStatsWithTimeFilterAndCinemaId(cinemaId, startDate, endDate);
    }
    
    public List<OrderTicketEntity> list(OrderTicketListInput input) {

        // 创建 QueryWrapper 对象
        QueryWrapper<OrderTicketEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(input.getOrderNo()), "o.order_no", input.getOrderNo());
        queryWrapper.eq("o.cinema_id", CinemaIdUtil.getCinemaIdFromRequest());
        queryWrapper.eq("o.type", 1);
        queryWrapper.orderByDesc("o.id");
        
        queryWrapper.ne("o.pay_status", 0);
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayStatusList()), "o.pay_status", input.getPayStatusList());
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayTypes()), "o.pay_type", input.getPayTypes());
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getStartDate() != null && input.getEndDate() != null) {
            LocalDateTime startOfDay = input.getStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("o.created_at", startOfDay, endOfDay);
        }
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getSessionStartDate() != null && input.getSessionEndDate() != null) {
            LocalDateTime startOfDay = input.getSessionStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getSessionEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("s.start_time", startOfDay, endOfDay);
        }
        
        
        queryWrapper.in(input.getUserIds().size() > 0, "ot.user_id", input.getUserIds());
        queryWrapper.in(input.getIds().size() > 0, "o.id", input.getIds());
        queryWrapper.isNull("o.deleted_at");
        return this.baseMapper.orderId(queryWrapper);
    }
}